﻿Imports GestionFicheClient.ModInterTerm
Imports GestionFicheClient.CnxMySql
Public Class gestionFiches

    Public Sub rafraichir_textbox()

        txtBoxNumInt.Text = ""
        DateTimePicker1.Text = ""
        cmbBoxTech.Text = ""
        cmbBoxTypeInt.Text = ""
        txtBoxCom.Text = ""
        txtBoxNomCli.Text = ""
        txtBoxTelCli.Text = ""
        txtBoxRef.Text = ""
        txtBoxIdent.Text = ""
        txtBoxMdp.Text = ""
        txtBoxFact.Text = ""

    End Sub
    Private Sub recharger_les_données()
        DataGridView1.DataSource = Executer_Requete_Select("Select idIntervention as Numero_Fiche, dateReception as Date_de_Reception,nomTechnicien as Technicien, typeIntervention as Travail_à_effectuer, commentaire, nomClient as Client,telclient as Telephone_Client, refEquipement as Equipement,identifiant, mdp as Mot_de_passe,adresseFacturation From interventions, techniciens, client where (Client.idClient = interventions.numclient) and (techniciens.idTechnicien = interventions.numtechnicien)")
        AlimenterComboBox(cmbBoxTech, "select * from techniciens", "nomTechnicien", "idTechnicien")
        AlimenterComboBox(cmbBoxTypeInt, "select * from typeintervention", "libTypeinter", "idtypeinter")

        Try
            txtBoxNumInt.Text = GetChamp_DataTable(DataGridView1.DataSource, 0, 0)
            DateTimePicker1.Text = GetChamp_DataTable(DataGridView1.DataSource, 0, 1)
            cmbBoxTech.Text = GetChamp_DataTable(DataGridView1.DataSource, 0, 2)

            Try

                Dim tinter = GetChamp_DataTable(DataGridView1.DataSource, 0, 3)
                Dim Tableau() As String
                Dim i As Integer

                'découpe la chaine en fonction des virgules ","
                'le résultat de la fonction Split est stocké dans un tableau
                Tableau = Split(tinter, ",")

                cmbBoxTypeInt.Text = Tableau(0)
                Dim chk As CheckBox
                Dim grp As Control


                For i = 1 To UBound(Tableau)

                    For Each grp In Me.GroupBox1.Controls

                        For Each ctrl As Control In grp.Controls

                            If TypeOf ctrl Is CheckBox Then

                                chk = ctrl

                                If Tableau(i) = ctrl.Text Then

                                    chk.Checked = True

                                End If
                            End If
                        Next ctrl
                    Next grp
                Next i

            Catch ex As Exception
                Dim tinter = GetChamp_DataTable(DataGridView1.DataSource, 0, 3)
                Dim Tableau() As String

                'découpe la chaine en fonction des espaces " "
                'le résultat de la fonction Split est stocké dans un tableau
                Tableau = Split(tinter, ",")

                cmbBoxTypeInt.Text = Tableau(0)

            End Try

            txtBoxCom.Text = GetChamp_DataTable(DataGridView1.DataSource, 0, 4)
            txtBoxNomCli.Text = GetChamp_DataTable(DataGridView1.DataSource, 0, 5)
            txtBoxTelCli.Text = GetChamp_DataTable(DataGridView1.DataSource, 0, 6)
            txtBoxRef.Text = GetChamp_DataTable(DataGridView1.DataSource, 0, 7)
            txtBoxIdent.Text = GetChamp_DataTable(DataGridView1.DataSource, 0, 8)
            txtBoxMdp.Text = GetChamp_DataTable(DataGridView1.DataSource, 0, 9)
            txtBoxFact.Text = GetChamp_DataTable(DataGridView1.DataSource, 0, 10)

        Catch
            MessageBox.Show("Il n'y a aucunes interventions en cours.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load



        grpBoxOs.Parent = GroupBox1

        grpBoxOs.Location = New Point(grpBoxOs.Location.Y = 30, 168)

        grpBoxPieces.Parent = GroupBox1
        grpBoxPieces.Location = New Point(grpBoxPieces.Location.Y = 30, 168)


        DateTimePicker1.Enabled = True
        cmbBoxTech.Enabled = True
        cmbBoxTypeInt.Enabled = True
        txtBoxCom.Enabled = True
        txtBoxNomCli.Enabled = True

        txtBoxRef.Enabled = True
        txtBoxIdent.Enabled = True
        txtBoxMdp.Enabled = True
        txtBoxFact.Enabled = True

        Me.MdiParent = FrmStart

        recharger_les_données()


        
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFermer.Click

        Me.Close()

    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick, DataGridView1.CellClick

        txtBoxNumInt.Text = GetChamp_DataTable(DataGridView1.DataSource, DataGridView1.CurrentCell.RowIndex, 0)
        DateTimePicker1.Text = GetChamp_DataTable(DataGridView1.DataSource, DataGridView1.CurrentCell.RowIndex, 1)
        cmbBoxTech.Text = GetChamp_DataTable(DataGridView1.DataSource, DataGridView1.CurrentCell.RowIndex, 2)

        Try

            Dim tinter = GetChamp_DataTable(DataGridView1.DataSource, DataGridView1.CurrentCell.RowIndex, 3)
            Dim Tableau() As String
            Dim i As Integer

            'découpe la chaine en fonction des espaces " "
            'le résultat de la fonction Split est stocké dans un tableau
            Tableau = Split(tinter, ",")

            cmbBoxTypeInt.Text = Tableau(0)
            Dim chk As CheckBox
            Dim grp As Control

            For Each grp In Me.GroupBox1.Controls

                For Each ctrl As Control In grp.Controls

                    If TypeOf ctrl Is CheckBox Then

                        chk = ctrl

                        chk.Checked = False

                    End If
                Next ctrl
            Next grp

            For i = 1 To UBound(Tableau)

                For Each grp In Me.GroupBox1.Controls

                    For Each ctrl As Control In grp.Controls

                        If TypeOf ctrl Is CheckBox Then

                            chk = ctrl

                            If Tableau(i) = ctrl.Text Then

                                chk.Checked = True

                            End If
                        End If
                    Next ctrl
                Next grp
            Next i

        Catch ex As Exception
            Dim tinter = GetChamp_DataTable(DataGridView1.DataSource, DataGridView1.CurrentCell.RowIndex, 3)
            Dim Tableau() As String

            'découpe la chaine en fonction des virgules ","
            'le résultat de la fonction Split est stocké dans un tableau
            Tableau = Split(tinter, ",")

            cmbBoxTypeInt.Text = Tableau(0)

        End Try

        If cmbBoxTypeInt.Text = "Réparation OS" Then
            grpBoxOs.Visible = True
        Else
            grpBoxOs.Visible = False

            If cmbBoxTypeInt.Text = "Remplacement Pièce" Then
                grpBoxPieces.Visible = True
            Else
                grpBoxPieces.Visible = False
            End If
        End If

        txtBoxCom.Text = GetChamp_DataTable(DataGridView1.DataSource, DataGridView1.CurrentCell.RowIndex, 4)
        txtBoxNomCli.Text = GetChamp_DataTable(DataGridView1.DataSource, DataGridView1.CurrentCell.RowIndex, 5)
        txtBoxTelCli.Text = GetChamp_DataTable(DataGridView1.DataSource, DataGridView1.CurrentCell.RowIndex, 6)
        txtBoxRef.Text = GetChamp_DataTable(DataGridView1.DataSource, DataGridView1.CurrentCell.RowIndex, 7)
        txtBoxIdent.Text = GetChamp_DataTable(DataGridView1.DataSource, DataGridView1.CurrentCell.RowIndex, 8)
        txtBoxMdp.Text = GetChamp_DataTable(DataGridView1.DataSource, DataGridView1.CurrentCell.RowIndex, 9)
        txtBoxFact.Text = GetChamp_DataTable(DataGridView1.DataSource, DataGridView1.CurrentCell.RowIndex, 10)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If txtBoxNumInt.Text = "" Then
            MessageBox.Show("Veuillez selectionner une intervention.", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else

            numInt = txtBoxNumInt.Text
            intDate = DateTimePicker1.Text
            Tech = cmbBoxTech.Text
            Type = cmbBoxTypeInt.Text
            Com = txtBoxCom.Text
            nomClient = txtBoxNomCli.Text
            telClient = txtBoxTelCli.Text
            refPc = txtBoxRef.Text
            Identifiant = txtBoxIdent.Text
            Mdp = txtBoxMdp.Text
            Adresse = txtBoxFact.Text
            Dim termin As New interTerminee
            termin.Show()
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim chk As CheckBox
        Dim grp As Control

        If Button3.Text = "Ajouter" Then
            rafraichir_textbox()
            Dim numeroint As String
            Dim test As Integer

            numeroint = Generer_Cle_Primaire("idIntervention", "Interventions")
            test = Executer_Requete_Scalaire("select count(numint) from archives where numint = " & numeroint)

            While (test <> 0)
                numeroint = numeroint + 1
                test = Executer_Requete_Scalaire("select count(numint) from archives where numint = " & numeroint)
            End While
            For Each grp In Me.GroupBox1.Controls

                For Each ctrl As Control In grp.Controls

                    If TypeOf ctrl Is CheckBox Then

                        chk = ctrl

                        chk.Checked = False

                    End If
                Next ctrl
            Next grp
            txtBoxNumInt.Text = numeroint
            Button3.Text = "Valider"
        Else
            If txtBoxNumInt.Text <> "" Then
                Try
                    Dim repcli = Executer_Requete_Scalaire("select count(idclient) from client where nomclient like '" & txtBoxNomCli.Text & "'")
                    If repcli = 1 Then
                        Dim rep = MessageBox.Show("Voulez vous vraiment ajouter cette intervention ?...", "Confirmation", MessageBoxButtons.YesNo)
                        If rep = DialogResult.Yes Then

                            Dim y = DateTimePicker1.Value.Year
                            Dim m = DateTimePicker1.Value.Month
                            Dim d = DateTimePicker1.Value.Day
                            Dim dateEntree = y & "-" & m & "-" & d

                            Dim ni, dt, tc, ti, com, nc, ref, id, md, ad As String
                            ni = txtBoxNumInt.Text
                            dt = dateEntree
                            tc = cmbBoxTech.SelectedIndex
                            ti = cmbBoxTypeInt.Text
                            For Each grp In Me.GroupBox1.Controls

                                For Each ctrl As Control In grp.Controls

                                    If TypeOf ctrl Is CheckBox Then

                                        chk = ctrl

                                        If chk.Checked = True And chk.Text <> "" Then

                                            ti = ti & "," & chk.Text

                                        End If
                                        If chk.Text = "" And chk.Checked = True Then

                                        End If
                                    End If
                                Next ctrl
                            Next grp
                            com = txtBoxCom.Text
                            nc = Executer_Requete_Scalaire("select idclient from client where nomclient like '" & txtBoxNomCli.Text & "'")

                            ref = txtBoxRef.Text
                            id = txtBoxIdent.Text
                            md = txtBoxMdp.Text
                            ad = txtBoxFact.Text

                            Executer_Requete_Action("Insert into Interventions Values (" & ni & ", '" & dt & "'," & tc & ",'" & ti & "','" & com & "'," & nc & ",'" & ref & "','" & id & "','" & md & "','" & ad & "')")

                            MessageBox.Show("Intervention Ajoutée...!", "Insertion", MessageBoxButtons.OK, MessageBoxIcon.Information)
                            DataGridView1.DataSource = Executer_Requete_Select("Select idIntervention as Numero_Fiche, dateReception as Date_de_Reception,nomTechnicien as Technicien, typeIntervention as Travail_à_effectuer, commentaire, nomClient as Client,telclient as Telephone_Client, refEquipement as Equipement,identifiant, mdp as Mot_de_passe,adresseFacturation From interventions, techniciens, client where (Client.idClient = interventions.numclient) and (techniciens.idTechnicien = interventions.numtechnicien)")

                            rafraichir_textbox()
                            'rafraichir()
                            Button5.Text = "Ajouter"
                        Else
                            Button5.Text = "Ajouter"
                            'rafraichir()
                        End If
                    Else
                        Dim repajoutclient = MessageBox.Show("Le client n'est pas enregistré, veuillez l'ajouter.", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                        If repajoutclient = DialogResult.OK Then
                            Dim ajcli As New gestionClient
                            ajcli.Show()

                        End If
                    End If
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "Alerte!", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End Try

            Else
                MessageBox.Show("Renseigner la Section SVP...!", "Alerte!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Executer_Requete_Action("")

    End Sub

    Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
        If CheckBox5.Checked Then
            txtBoxAutre.Enabled = True
            txtBoxAutre.Text = ""
        Else
            txtBoxAutre.Enabled = False
            txtBoxAutre.Text = "Autre"
        End If
    End Sub

    Private Sub cmbBoxTypeInt_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBoxTypeInt.SelectedIndexChanged

        If cmbBoxTypeInt.Text = "Réparation OS" Then
            grpBoxOs.Visible = True
        Else
            grpBoxOs.Visible = False

            If cmbBoxTypeInt.Text = "Remplacement Pièce" Then
                grpBoxPieces.Visible = True
            Else
                grpBoxPieces.Visible = False
            End If
        End If
    End Sub

    Private Sub CheckBox9_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox9.CheckedChanged
        If CheckBox9.Checked Then
            txtBoxAutre.Parent = grpBoxOs
            txtBoxAutre.Location = New Point(txtBoxAutre.Location.Y = 6, 101)

            txtBoxAutre.Text = ""
        Else
            txtBoxAutre.Enabled = False
            txtBoxAutre.Text = "Autre"
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim chk As CheckBox
        Dim grp As Control

        If Button5.Text = "Modifier" Then
            Button5.Text = "Valider"

        Else
            Try
                Dim repcli = Executer_Requete_Scalaire("select count(idclient) from client where nomclient like '" & txtBoxNomCli.Text & "'")
                If repcli = 1 Then
                    Dim rep = MessageBox.Show("Voulez vous vraiment modifier cette intervention ?...", "Confirmation", MessageBoxButtons.YesNo)
                    If rep = DialogResult.Yes Then

                        Dim y = DateTimePicker1.Value.Year
                        Dim m = DateTimePicker1.Value.Month
                        Dim d = DateTimePicker1.Value.Day
                        Dim dateEntree = y & "-" & m & "-" & d

                        Dim ni, dt, tc, ti, com, nc, ref, id, md, ad As String
                        ni = txtBoxNumInt.Text
                        dt = dateEntree
                        tc = cmbBoxTech.SelectedIndex
                        ti = cmbBoxTypeInt.Text
                        For Each grp In Me.GroupBox1.Controls

                            For Each ctrl As Control In grp.Controls

                                If TypeOf ctrl Is CheckBox Then

                                    chk = ctrl

                                    If chk.Checked = True And chk.Text <> "" Then

                                        ti = ti & "," & chk.Text

                                    End If
                                    If chk.Text = "" And chk.Checked = True Then

                                    End If

                                End If
                            Next ctrl
                        Next grp
                        com = txtBoxCom.Text
                        nc = Executer_Requete_Scalaire("select idclient from client where nomclient like '" & txtBoxNomCli.Text & "'")

                        ref = txtBoxRef.Text
                        id = txtBoxIdent.Text
                        md = txtBoxMdp.Text
                        ad = txtBoxFact.Text

                        Executer_Requete_Action("UPDATE `interventions` SET `dateReception`='" & dt & "',`numTechnicien`=" & tc & ",`typeIntervention`='" & ti & "',`commentaire`='" & com & "',`numClient`=" & nc & ",`refEquipement`='" & ref & "',`identifiant`='" & id & "',`mdp`='" & md & "',`adresseFacturation`='" & ad & "' WHERE idIntervention = " & ni)
                        MessageBox.Show("Intervention modifiée...!", "Insertion", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        DataGridView1.DataSource = Executer_Requete_Select("Select idIntervention as Numero_Fiche, dateReception as Date_de_Reception,nomTechnicien as Technicien, typeIntervention as Travail_à_effectuer, commentaire, nomClient as Client,telclient as Telephone_Client, refEquipement as Equipement,identifiant, mdp as Mot_de_passe,adresseFacturation From interventions, techniciens, client where (Client.idClient = interventions.numclient) and (techniciens.idTechnicien = interventions.numtechnicien)")

                        rafraichir_textbox()
                        'rafraichir()
                        Button5.Text = "Modifier"
                    Else
                        Button5.Text = "Modifier"
                        'rafraichir()
                    End If
                Else
                    Dim repajoutclient = MessageBox.Show("Le client n'est pas enregistré, veuillez l'ajouter.", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    If repajoutclient = DialogResult.OK Then
                        Dim ajcli As New gestionClient
                        ajcli.Show()

                    End If
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Alerte!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub

    Private Sub Button6_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim repsupp = MessageBox.Show("Souhaitez vous vraiment supprimer  cette intervention ?", "Suppression", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        Dim ni As Integer
        ni = txtBoxNumInt.Text
        If repsupp = DialogResult.Yes Then

            Executer_Requete_Action("DELETE FROM `interventions` WHERE idIntervention = " & ni)
            recharger_les_données()


        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim arc As New affArchives
        arc.Show()

    End Sub

    Private Sub txtRech_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtRech.TextChanged
        If txtRech.Text <> "" Then


            Try
                Label11.Text = "Recherche par numero :"
                Label11.ForeColor = Color.Black

                DataGridView1.DataSource = Executer_Requete_Select("Select idIntervention as Numero_Fiche, dateReception as Date_de_Reception,nomTechnicien as Technicien, typeIntervention as Travail_à_effectuer, commentaire, nomClient as Client,telclient as Telephone_Client, refEquipement as Equipement,identifiant, mdp as Mot_de_passe,adresseFacturation From interventions, techniciens, client where (Client.idClient = interventions.numclient) and (techniciens.idTechnicien = interventions.numtechnicien) and idintervention = " & txtRech.Text)
            Catch ex As Exception
                Label11.Text = "Aucunes interventions correspondantes"
                Label11.ForeColor = Color.Red
            End Try
        End If
    End Sub
End Class
